import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * The type Refrigerator frame.
 *
 * @author Sihan Wang
 * @date 24/06/09
 */
public class RefrigeratorFrame extends JFrame implements ActionListener {
    /**
     * The Back.
     */
    public JButton back = new JButton("Back");
    /**
     * The Edit food.
     */
    public JButton EditFood = new JButton("Edit Food");
    public JButton CleanFood = new JButton("Clean All Food");
    /**
     * The Button panel.
     */
    public JPanel buttonPanel = new JPanel();
    /**
     * The Panel.
     */
    public JPanel panel = new JPanel();
    /**
     * The Background label.
     */
    public JLabel backgroundLabel = new JLabel();
    /**
     * The Display area.
     */
    public JTextArea displayArea;

    /**
     * Instantiates a new Refrigerator frame.
     */
    public RefrigeratorFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Interface();
                element();
            }
        });
        this.setVisible(true);
    }

    /**
     * Interface.
     */
    public void Interface() {
        this.setTitle("Refrigerator page");
        this.pack();
        this.setSize(new Dimension(1200, 675));
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.getContentPane().add(buttonPanel);
        this.setLayout(new BorderLayout());

        Path filePath = Paths.get(System.getProperty("user.dir"), "resources/refrigerator.png");
        String refrigeratorFilePath = filePath.toString();

        backgroundLabel = new JLabel(new ImageIcon(refrigeratorFilePath));
        backgroundLabel.setBounds(0, 0, 1200, 675);
        backgroundLabel.setLayout(new BorderLayout());
        backgroundLabel.add(buttonPanel);
        backgroundLabel.setVisible(true);
        panel.add(backgroundLabel);
        this.getContentPane().add(panel);
    }

    /**
     * Element.
     */
    public void element() {
        back.setBounds(25, 25, 80, 80);
        back.setFont(new Font("Posterama", Font.PLAIN, 20));
        back.setBackground(Color.orange);
        back.addActionListener(this);

        EditFood.setBounds(890, 375, 150, 60);
        EditFood.setFont(new Font("Posterama", Font.BOLD, 20));
        EditFood.setOpaque(false);
        EditFood.setBackground(new Color(255, 255, 255, 128));
        EditFood.setForeground(Color.BLACK);
        EditFood.setFocusPainted(false);
        EditFood.setContentAreaFilled(false);
        EditFood.setBorderPainted(false);
        EditFood.addActionListener(this);

        CleanFood.setBounds(880, 460, 180, 60);
        CleanFood.setFont(new Font("Posterama", Font.BOLD, 20));
        CleanFood.setBackground(Color.orange);
        CleanFood.addActionListener(this);


        displayArea = new JTextArea(9, 10);
        String foodLest = Main.fc1.printAllFoodInfo();
        displayArea.setText(foodLest);
        displayArea.setLineWrap(true);
        displayArea.setWrapStyleWord(true);
        displayArea.setOpaque(false);
        displayArea.setBackground(new Color(0, 0, 0, 0));
        displayArea.setFont(new Font("Posterama", Font.BOLD, 24));
        displayArea.setCaretPosition(0);
        displayArea.setBorder(null);

        JScrollPane scrollPane = new JScrollPane(displayArea);
        scrollPane.setBounds(170, 135, 600, 400);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setBorder(null);

        buttonPanel.setLayout(null);
        buttonPanel.setOpaque(false);
        buttonPanel.add(back);
        buttonPanel.add(EditFood);
        buttonPanel.add(CleanFood);
        buttonPanel.add(scrollPane);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        Object obj = e.getSource();

        if (obj == back) {
            this.setVisible(false);
            new MenuFrame();
        }
        if (obj == EditFood) {
            this.setVisible(false);
            new EditFoodDialog();
        }
        if (obj == CleanFood){
            Main.fc1.cleanCollection();
            this.setVisible(false);
            new RefrigeratorFrame();
        }
    }
}
